#include "student.h"
#include "login.h"

int menu_choose() {
	int choice;
	puts("╭──────────────── Student Manager ────────────────╮");
	puts("│ 1. Add student             2. Display students  │");
	puts("│ 3. Modify student          4. Delete student    │");
	puts("│ 5. Find student            6. Analysis          │");
	puts("│ 7. Sort student            8. Export as roster  │");
	puts("│ 9. Import from roster      10.Save bin data     │");
	puts("│ 11.Read bin data           12.Exit              │");
	puts("╰─────────────────────────────────────────────────╯");
	printf("Choose function:");
	
	while (1) {
		scanf("%d", &choice);
		clearstdin(); // 清理缓冲区残留
		if (choice >= 1 && choice <= 12) {
			break;
		} else {
			puts("Input again!(1-12)");
			printf("Choose function:");
		}
	}
	return choice;
}


int main() {
	if (!login()) {
		printf("Login failed, exit...\n");
		return 1;
	}
	table StudentsTable = {};
	
	while (1) {
		int choice = menu_choose();
		switch (choice) {
			case 1: st_add(StudentsTable); break;
			case 2: st_show(StudentsTable); break;
			case 3: st_mod(StudentsTable); break;
			case 4: st_del(StudentsTable); break;
			case 5: st_find(StudentsTable); break;
			case 6: puts("// 分析成绩情况"); break;
			case 7: puts("// 按成绩排序"); break;
			case 8: puts("// 从花名册导入学生"); break;
			case 9: puts("// 导出学生名册"); break;
			case 10: st_save(StudentsTable); break;
			case 11: st_read(StudentsTable); break;
			
			default:
			puts("Exit...");
			goto clean_and_exit; // 跳出 while 循环
		}
	}
	// 清理资源
	clean_and_exit:
	Student_freeall(StudentsTable);
	String.freeall();
	return 0;
}